﻿/* Copyright (c) Microsoft Corporation
 * 
 * All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.  You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 * 
 * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProximityTapper
{
    [CLSCompliant(false)]
    public class StringFormatter
    {
        /// <summary>
        /// Converts byte array into hex string e.g. byte[] data = {0x12, 0x34, 0x56}; is shown as 
        /// "{0x12, 0x34, 0x56}"
        /// </summary>
        /// <param name="data">Array containing data</param>
        /// <returns>String displaying byte array in hex format</returns>
        public static string ToHexString(byte[] data)
        {
            return "{0x" + BitConverter.ToString(data).Replace("-", ", 0x") + "}";
        }

        /// <summary>
        /// Converts a number into hex string e.g. UInt32 value = 10 is shown as "0xA"
        /// </summary>
        /// <param name="value">Value</param>
        /// <returns>String displaying number in hex format</returns>
        public static string ToHexString(UInt32 value)
        {
            return String.Format("0x{0:X}", value);
        }

        /// <summary>
        /// Converts a number into hex string e.g. int value = 10 is shown as "0xA"
        /// </summary>
        /// <param name="value">Value</param>
        /// <returns>String displaying number in hex format</returns>
        public static string ToHexString(int value)
        {
            return String.Format("0x{0:X}", value);
        }

        /// <summary>
        ///     Gets a byte array as address string 
        ///     e.g. fe80:00:00:00:206e:eb71:85bc:e633
        /// </summary>
        /// <param name="data">Array containing data</param>
        /// <returns>String displaying byte array as address</returns>
        public static string ToAddressString(byte[] data)
        {
            string output = "";

            for (UInt32 counter = 0; counter < data.Length; counter++)
            {
                output += String.Format("{0:X}", data[counter]);

                if (1 == (counter % 2) && (counter != (data.Length - 1)))
                {
                    output += ":";
                }
            }

            return output;
        }
    }
}
